package com.zhu.day.learn;

import com.alibaba.fastjson.JSON;

import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCache2 {
    private Map<Integer, Integer> map;
    private final int capacity;

    public LRUCache2(int capacity) {
        this.capacity = capacity;
        map = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true) {
            @Override
            protected boolean removeEldestEntry(Map.Entry entry) {
                return size() > capacity;
            }
        };

    }

    public int get(int key) {
        return map.getOrDefault(key, -1);
    }

    public void put(int key, int value) {
        map.put(key, value);
    }


    public static void main(String[] args) {
        LRUCache2 lruCache = new LRUCache2(3);
        for (int i = 0; i < 10; i++) {
            lruCache.put(i, i);
            if (i < 5) {
                System.out.println(lruCache.get(i));
            }
        }
        System.out.println(JSON.toJSONString(lruCache.map));


    }
}
